package com.own.stu.apm.plugins.springmvc;

import com.own.stu.apm.agent.core.plugin.interceptor.ConstructorMethodsInterceptorPoint;
import com.own.stu.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptorPoint;
import com.own.stu.apm.agent.core.plugin.interceptor.StaticMethodsInterceptorPoint;
import com.own.stu.apm.agent.core.plugin.interceptor.enhence.ClassEnhancePluginDefine;
import com.own.stu.apm.agent.core.plugin.match.ClassMatch;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.*;

public abstract class SpringmvcInstrumentation extends ClassEnhancePluginDefine {

    private static String mapping_preffix = "org.springframework.web.bind.annotation";
    private static String mapping_suffix = "Mapping";
    private static final String INTERCEPTOR = "com.own.stu.apm.plugins.springmvc.interceptor.SpringmvcInterceptor";

    protected abstract ClassMatch enhanceClass();

    protected InstanceMethodsInterceptorPoint[] getInstanceMethodsInterceptorPoint() {
        return new InstanceMethodsInterceptorPoint[]{
                new InstanceMethodsInterceptorPoint() {

                    public ElementMatcher<MethodDescription> getMethodMatcher() {
                        return not(isStatic())
                                .and(isAnnotatedWith(
                                        nameStartsWith(mapping_preffix).and(nameEndsWith(mapping_suffix))
                                ));
                    }

                    public String getMethodInterceptor() {
                        return INTERCEPTOR;
                    }
                }
        };
    }

    protected ConstructorMethodsInterceptorPoint[] getConstructorMethodsInterceptorPoint() {
        return new ConstructorMethodsInterceptorPoint[0];
    }

    protected StaticMethodsInterceptorPoint[] getStaticMethodsInterceptorPoint() {
        return new StaticMethodsInterceptorPoint[0];
    }
}
